<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" th:with="title='文章管理',active='comments'">
<header th:replace="back/header::headerFragment(${title},${active})"></header>
<head>
  <meta th:name="_csrf" th:content="${_csrf.token}"/>
  <!-- 默认的header name是X-CSRF-TOKEN -->
  <meta th:name="_csrf_header" th:content="${_csrf.headerName}"/>
</head>
<body class="fixed-left">
<div id="wrapper">
  <div th:replace="back/header::header-body"></div>
  <div class="content-page">
    <div class="content">
      <div class="container">
        <div class="row">
          <div class="col-sm-12">
            <h4 class="page-title">文章管理</h4>
          </div>
          <div class="col-md-12">
            <table class="table table-striped table-bordered">
              <thead>
              <tr>
                <th width="35%">文章标题</th>
                <th>分类</th>
                <th>标签</th>
<!--                <th>评论状态</th>-->

              </tr>
              </thead>
              <tbody>
              <th:block th:each="article : ${articles}">
                <tr th:id="${article.id}">
                  <td>
                    <a th:href="@{'/article/'+${article.id}}" th:text="${article.title}" target="_blank"></a>
                  </td>
                  <td><a th:href="@{'/comments/see/'+${article.id}}" th:text="${article.categories}"></a></td>
                  <td><a th:href="@{'/comments/see/'+${article.id}}" th:text="${article.tags}"></a></td>
<!--                  <td>-->
<!--                    <a href="javascript:void(0)" th:onclick="'openCom('+${article.id}+');'"-->
<!--                       class="btn btn-primary btn-sm waves-effect waves-light m-b-5">-->
<!--                      <i class="fa fa-edit"></i> <span>开启</span></a>-->
<!--                    <a href="javascript:void(0)" th:onclick="'closeCom('+${article.id}+');'"-->
<!--                       class="btn btn-danger btn-sm waves-effect waves-light m-b-5">-->
<!--                      <i class="fa fa-trash-o"></i> <span>屏蔽</span></a>-->
<!--                    <a href="javascript:void(0)" th:onclick="'delArticle('+${article.id}+');'"-->
<!--                       class="btn btn-warning btn-sm waves-effect waves-light m-b-5" >-->
<!--                      <i class="fa fa-rocket"></i> <span>删帖</span></a>-->
<!--                  </td>-->
                </tr>
              </th:block>

              </tbody>
            </table>
            <div th:replace="comm/paging :: pageAdminNav(${articles})"></div>
          </div>
        </div>
        <div th:replace="back/footer :: footer-content"></div>
      </div>
    </div>
  </div>
</div>
<div th:replace="back/footer :: footer"></div>
<script type="text/javascript">
  function openCom(id) {
    // 获取<meta>标签中封装的_csrf信息
    var token = $("meta[name='_csrf']").attr("content");
    var header = $("meta[name='_csrf_header']").attr("content");
    if(confirm('确定开启吗?')){
      $.ajax({
        type:'put',
        url : '/admin/opencom',
        data: {id:id},
        dataType: 'json',
        beforeSend : function(xhr) {
          xhr.setRequestHeader(header, token);
        },
        success: function (result) {
          if (result && result.success) {
            window.alert("开启成功");
            window.location.reload();
          } else {
            window.alert(result.msg || '开启失败')
          }
        }
      });
    }
  }
  function delArticle(id) {
    // 获取<meta>标签中封装的_csrf信息
    var token = $("meta[name='_csrf']").attr("content");
    var header = $("meta[name='_csrf_header']").attr("content");
    if(confirm('确定删除该文章吗?')){
      $.ajax({
        type:'post',
        url : '/admin/article/delete',
        data: {id:id},
        dataType: 'json',
        beforeSend : function(xhr) {
          xhr.setRequestHeader(header, token);
        },
        success: function (result) {
          if (result && result.success) {
            window.alert("文章删除成功");
            window.location.reload();
          } else {
            window.alert(result.msg || '文章删除失败')
          }
        }
      });
    }
  };

  function closeCom(id) {
    // 获取<meta>标签中封装的_csrf信息
    var token = $("meta[name='_csrf']").attr("content");
    var header = $("meta[name='_csrf_header']").attr("content");
    if(confirm('确定关闭吗?')){
      $.ajax({
        type:'put',
        url : '/admin/closecom',
        data: {id:id},
        dataType: 'json',
        beforeSend : function(xhr) {
          xhr.setRequestHeader(header, token);
        },
        success: function (result) {
          if (result && result.success) {
            window.alert("关闭成功");
            window.location.reload();
          } else {
            window.alert(result.msg || '关闭失败')
          }
        }
      });
    }
  }
</script>
</body>
</html>